package com.student.dms;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.rxjava3.EmptyResultSetException;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.FixReportDao;
import com.student.db.FixReportEntity;
import com.student.db.PaymentDao;
import com.student.db.PaymentEntity;
import com.student.db.RoomDao;
import com.student.db.RoomEntity;
import com.student.dms.databinding.ActivityPaymentBinding;
import com.student.dms.databinding.ActivityRegisterBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class PaymentActivity extends BaseActivity {

    private ActivityPaymentBinding activityPaymentBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityPaymentBinding = ActivityPaymentBinding.inflate(getLayoutInflater());
        setContentView(activityPaymentBinding.getRoot());
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        String username = sharedPreferences.getString("username", "");
        AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
        AccountDao accountDao = roomDatabase.provideAccountDao();
        Single<AccountEntity> accountFlow = accountDao.searchAccountByName(username);
        activityPaymentBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        activityPaymentBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disposable.add(accountFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
                    String editRoomNo = activityPaymentBinding.editRoomNo.getText().toString().trim();
                    String eidtPayNum = activityPaymentBinding.eidtPayNum.getText().toString().trim();
                    if (checkInputIsNull(editRoomNo) || checkInputIsNull(eidtPayNum) || checkInputIsNull(activityPaymentBinding.rgSelectType)) {
                        ToastUtils.showToast(PaymentActivity.this, "水电费类型或金额不能为空！");
                        return;
                    }
                    AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
                    RoomDao roomDao = roomDatabase.provideRoomDao();
                    Single<RoomEntity> roomFlow = roomDao.searchRooms(editRoomNo);
                    PaymentDao paymentDao = roomDatabase.providePaymentDao();
                    PaymentEntity paymentEntity = new PaymentEntity();

                    RadioButton selectedRadioButton = findViewById(activityPaymentBinding.rgSelectType.getCheckedRadioButtonId());
                    int type;
                    if (selectedRadioButton.getText().equals("水费")) {
                        type = 1;
                    } else {
                        type = 2;
                    }
                    paymentEntity.setName(accountEntity.getName());
                    paymentEntity.setUsername(accountEntity.getUserName());
                    paymentEntity.setPayType(type);
                    paymentEntity.setRoomNo(editRoomNo);
                    paymentEntity.setPayment(eidtPayNum);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String time = simpleDateFormat.format(new Date());
                    paymentEntity.setPayTime(time);
                    disposable.add(roomFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(roomEntity -> {
                        disposable.add(paymentDao.insert(paymentEntity).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(() -> {
                            ToastUtils.showToast(PaymentActivity.this, "缴费成功！");
                            PaymentActivity.this.finish();
                        }));
                    }, roomFlowThrowable -> {
                        //寝室没人直接插入
                        if (roomFlowThrowable instanceof EmptyResultSetException) {
                            //账号表为空
                            ToastUtils.showToast(PaymentActivity.this, "宿舍不存在！");
                        }
                    }));
                }, insertstudentDaothrowable -> {
                    ToastUtils.showToast(PaymentActivity.this, "提交成功！");
                }));
            }
        });
    }

    private boolean checkInputIsNull(RadioGroup radioGroup) {
        return radioGroup.getCheckedRadioButtonId() == -1;
    }

    private boolean checkInputIsNull(String text) {
        return TextUtils.isEmpty(text);
    }
}
